########################################################
###### Replication Code for Figure 2 and Table A6 ######
################## January 9th, 2025 ################### 
########################################################
rm(list=ls())

library(dplyr)
library(fixest)
library(ggplot2)
library(stringr)
library(hbal)
library(gt)
library(modelsummary)

df <- readRDS("data/workfile.rds")


w <- readRDS("data/weighted_party_feeling.rds")

df$parfam_nat <- ifelse(df$parfam_name == "Nationalist parties", 1, 0)
df$parfam_left <- ifelse(df$parfam_name == "Socialist/left parties", 1, 0)
df$parfam_con <- ifelse(df$parfam_name == "Conservative parties", 1, 0)

df$out_feeling <- scale(df$mean_outparty_feeling) + scale(df$mean_close_num)

df <- df %>% left_join(w)

control_variables <- c('high_pol_interest',
                       "high_edu", "medium_edu", "low_edu",  "dem_age",
                       "parfam_nat", "mean_close_num", "mean_outparty_feeling",
                       "coDE", "coFR", "coES", "coNL", "coUS", "coPL", 
                       "coGR", "coIT", "coSE", "d_affiliated", 
                       "high_income", "medium_income", "low_income")

df$trust_in_opposing_parties = scale(df$trust_in_opposing_parties)

nomiss_df <- df %>% 
  select(open_immig, dem_female, dem_country_code, dem_education_level,
         dem_country_code, dem_rural, dem_age, lrscale, parfam_name,
         high_income, medium_income, low_income, political_interest_num,
         high_edu, medium_edu, low_edu, treated, mean_outparty_feeling, 
         high_pol_interest, mean_close_num, d_affiliated, treated,
         political_interest, treated_econ, treated_culture, out_feeling,
         trust_in_opposing_parties) %>% 
  mutate(coDE = ifelse(dem_country_code == "DE", 1, 0), 
         coFR = ifelse(dem_country_code == "FR", 1, 0), 
         coNL = ifelse(dem_country_code == "NL", 1, 0),
         coUS = ifelse(dem_country_code == "US", 1, 0),
         coPL = ifelse(dem_country_code == "PL", 1, 0),
         coGR = ifelse(dem_country_code == "GR", 1, 0), 
         coIT = ifelse(dem_country_code == "IT", 1, 0), 
         coSE = ifelse(dem_country_code == "SE", 1, 0), 
         coGB = ifelse(dem_country_code == "GB", 1, 0), 
         coES = ifelse(dem_country_code == "ES", 1, 0), 
         parfam_liberal = ifelse(parfam_name == "Liberal parties", 1, 0), 
         parfam_other = ifelse(parfam_name == "Other parties", 1, 0), 
         parfam_eco = ifelse(parfam_name == "Ecological parties", 1, 0), 
         parfam_nat = ifelse(parfam_name == "Nationalist parties", 1, 0), 
         parfam_left = ifelse(parfam_name == "Socialist/left parties", 1, 0), 
         parfam_cd = ifelse(parfam_name == "Christian democratic parties", 1, 0), 
         parfam_nonvoters = ifelse(parfam_name == "Non-voters", 1, 0), 
         parfam_sd = ifelse(parfam_name == "Social democratic parties", 1, 0), 
         parfam_con = ifelse(parfam_name == "Conservative parties", 1, 0), 
         parfam_ethnic = ifelse(parfam_name == "Ethnic and regional parties", 1, 0),
         parfam_spec = ifelse(parfam_name == "Special issue party", 1, 0),) %>% 
  .[complete.cases(.), ]

coef_df <- data.frame()

########## Culture - No immigration
nomiss <- nomiss_df %>% 
  filter((open_immig == 0 & treated_culture == 1) | treated == 0) 
  
hbal.out <- hbal(Treat = 'treated', 
                 X = control_variables,  
                 Y = 'trust_in_opposing_parties',  data = nomiss)
m1 = att(hbal.out, dr = FALSE)
no_discussion_culture <- m1


temp <- data.frame(coef = c(m1$Estimate[1]),
                   se = c(m1$`Std. Error`[1]), 
                   name = "Did not discuss immigration",
                   var = "Trust", 
                   group = "Cultural issues \n condition")


coef_df <- rbind.data.frame(coef_df, temp)



########## Culture - Immigration
nomiss <- nomiss_df %>% 
  filter((open_immig == 1 & treated_culture == 1) | treated == 0) 

hbal.out <- hbal(Treat = 'open_immig', 
                 X = control_variables,  
                 Y = 'trust_in_opposing_parties',  data = nomiss)
m1 = att(hbal.out, dr = FALSE)
discussion_culture <- m1

temp <- data.frame(coef = c(m1$Estimate[1]),
                   se = c(m1$`Std. Error`[1]), 
                   name = "Discussed immigration",
                   var = "Trust", 
                   group = "Cultural issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)



########## Economics - Immigration
nomiss <- nomiss_df %>% 
  filter((open_immig == 1 & treated_econ == 1) | treated == 0) 

hbal.out <- hbal(Treat = 'open_immig', 
                 X = control_variables,  
                 Y = 'trust_in_opposing_parties',  data = nomiss)
m1 = att(hbal.out, dr = FALSE)
discussion_econ <- m1

temp <- data.frame(coef = c(m1$Estimate[1]),
                   se = c(m1$`Std. Error`[1]), 
                   name = "Discussed immigration",
                   var = "Trust", 
                   group = "Economic issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp)


########## Economics - No immigration
nomiss <- nomiss_df %>% 
  filter((open_immig == 0 & treated_econ == 1) | treated == 0) 

hbal.out <- hbal(Treat = 'treated', 
                 X = control_variables,   
                 Y = 'trust_in_opposing_parties',  data = nomiss)
m1 = att(hbal.out, dr = FALSE)
no_discussion_econ <- m1

temp <- data.frame(coef = c(m1$Estimate[1]),
                   se = c(m1$`Std. Error`[1]), 
                   name = "Did not discuss immigration",
                   var = "Trust", 
                   group = "Economic issues \n condition")

coef_df <- rbind.data.frame(coef_df, temp) %>% 
  mutate(name = ifelse(name == "Did not discuss immigration", "No immigration", name),
         name = ifelse(name == "Discussed immigration", "Immigration", name))



pd <- position_dodge(0.5)

coef_df <- coef_df %>% filter(var == "Trust")
coef_df1 = coef_df

######## Figure 2

regs <- ggplot(coef_df, aes(x = group,  y = coef,  color = name)) + 
  geom_point(position = pd, shape = 21, fill = 'white') + 
  geom_errorbar(aes( ymin = coef - se*1.959964, ymax = coef + se*1.959964), width = 0, position = pd) + 
  geom_errorbar(aes( ymin = coef - se*1.644854, ymax = coef + se*1.644854), width = 0, position = pd, linewidth = 1.2) + 
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  scale_colour_manual(values = c("black", "grey")) + 
  xlab("") + 
  ylab("Effect of discussing immigration on out-partisan trust") +
  coord_flip() +
  theme_bw() + 
  guides(color=guide_legend(title=""))

regs
ggsave("figures/Fig2_entropy_balancing_predictors.pdf",
       height = 4, width = 6)



###### Table A6
no_discussion_econ$term <- rownames(no_discussion_econ)
no_discussion_econ$std.error <- no_discussion_econ$`Std. Error`
no_discussion_econ$p.value <- no_discussion_econ$`Pr(>|t|)`
no_discussion_econ$estimate <- no_discussion_econ$Estimate
rownames(no_discussion_econ) <- NULL

gl <- data.frame(
  Num.Obs. = no_discussion_econ$DF + 2)
no_discussion_econ <- list(
  tidy = no_discussion_econ[, 8:11],
  glance = gl)
class(no_discussion_econ) <- "modelsummary_list"

modelsummary(no_discussion_econ, stars = T)

discussion_econ$term <- rownames(discussion_econ)
discussion_econ$std.error <- discussion_econ$`Std. Error`
discussion_econ$p.value <- discussion_econ$`Pr(>|t|)`
discussion_econ$estimate <- discussion_econ$Estimate
rownames(discussion_econ) <- NULL

gl <- data.frame(
  Num.Obs. = discussion_econ$DF + 2)
discussion_econ <- list(
  tidy = discussion_econ[, 8:11],
  glance = gl)
class(discussion_econ) <- "modelsummary_list"
modelsummary(discussion_econ, stars = T)

no_discussion_culture$term <- rownames(no_discussion_culture)
no_discussion_culture$std.error <- no_discussion_culture$`Std. Error`
no_discussion_culture$p.value <- no_discussion_culture$`Pr(>|t|)`
no_discussion_culture$estimate <- no_discussion_culture$Estimate
rownames(no_discussion_culture) <- NULL

gl <- data.frame(
  Num.Obs. = no_discussion_culture$DF + 2)
no_discussion_culture <- list(
  tidy = no_discussion_culture[, 8:11],
  glance = gl)
class(no_discussion_culture) <- "modelsummary_list"
modelsummary(no_discussion_culture, stars = T)


discussion_culture$term <- rownames(discussion_culture)
discussion_culture$std.error <- discussion_culture$`Std. Error`
discussion_culture$p.value <- discussion_culture$`Pr(>|t|)`
discussion_culture$estimate <- discussion_culture$Estimate
rownames(discussion_culture) <- NULL

gl <- data.frame(
  Num.Obs. = discussion_culture$DF + 2)
discussion_culture <- list(
  tidy = discussion_culture[, 8:11],
  glance = gl)
class(discussion_culture) <- "modelsummary_list"
modelsummary(discussion_culture, stars = T)

modellist <- list(no_discussion_econ, discussion_econ, no_discussion_culture, discussion_culture)
modelsummary(modellist, stars = T) 
m_sum <- modelsummary(modellist, stars = T, 
             title = "Treatment effect on out-partisan trust",
             statistic = c("std.error",
                           "p = {p.value}"),
             #gof_omit = "BIC|AIC|r2.within|r2.within.adjusted|rmse|std.error.type|se_type",
             coef_rename = c("treated" = "Did not discuss immigration", 
                             "open_immig" = "Discussed immigration", 
                             "mean_outparty_feeling" = "Average outpartisan feeling", 
                             "high_pol_interest" = "High political interest"),
             output = "gt")

m_sum %>% tab_spanner(label = 'Economic issues', columns = 2:3) %>% 
  tab_spanner(label = 'Cultural issues', columns = 4:5) %>% 
  gtsave("tables/TabA6_balancing_predictors.docx")


